package Servlet;

import Dao.doctorDaoImpl;
import bean.Doctor;
import utils.Age;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/DoctorManagementServlet")

public class DoctorManagementServlet extends BaseServlet{
        doctorDaoImpl doctorDao = new doctorDaoImpl();

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<Doctor> doctorList = doctorDao.queryDoctors();
        req.setAttribute("doctorList",doctorList);
        req.getRequestDispatcher("/DoctorManagement.jsp").forward(req,resp);
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("我是update");
        String did = req.getParameter("did");
        String dname = req.getParameter("name");
        String dsex = req.getParameter("sex");
        String date = req.getParameter("birthday");
        int age = Age.AgeOfAcquisition(date);
        int dfee = Integer.parseInt(req.getParameter("fee"));
        String room = req.getParameter("room");
        String office = req.getParameter("department");
        String description = req.getParameter("text");
        String ability = req.getParameter("ability");
        Doctor doctor = new Doctor(did,null,null,dname,dfee,dsex,age,room,office,null,description,ability,null);
        doctorDao.updateDoctor(doctor);
        resp.sendRedirect("http://localhost:8080/Hospital_line_call_system_war/DoctorManagementServlet?action=list");


    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                System.out.println("delete");
                String did = req.getParameter("did");
                doctorDao.deleteDoctorBYId(Integer.parseInt(did));
                req.getRequestDispatcher("/DoctorManagementServlet?action=list").forward(req,resp);

    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("Stringg name");
            String dname = req.getParameter("name");
            String dsex = req.getParameter("sex");
            String date = req.getParameter("birthday");
            int age = Age.AgeOfAcquisition(date);
            int dfee = Integer.parseInt(req.getParameter("fee"));
            String room = req.getParameter("room");
            String office = req.getParameter("department");
            String description = req.getParameter("text");
            String ability = req.getParameter("ability");

            Doctor doctor = new Doctor(null,null,null,dname,dfee,dsex,age,room,office,null,description,ability,null);

            doctorDao.addDoctor(doctor);
            resp.sendRedirect("http://localhost:8080/Hospital_line_call_system_war/DoctorManagementServlet?action=list");

    }

    protected void getDoctor(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String did = req.getParameter("did");
        Doctor curdoctor = doctorDao.queryDoctorById(Integer.parseInt(did));
        req.setAttribute("curdoctor",curdoctor);
        req.getRequestDispatcher("/manager/addDoctor.jsp").forward(req,resp);
    }


}
